package com.ruibang.glass.material.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruibang.glass.material.entity.MaterialLotItem;
import com.ruibang.glass.material.service.MaterialLotItemService;
import org.springframework.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.teaming.cloud.framework2.common.model.ResultBody;
import com.teaming.cloud.framework2.common.pojo.PageRequest;
import com.teaming.cloud.framework2.common.pojo.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.CollectionUtils;
import lombok.AllArgsConstructor;

import java.util.*;
import lombok.Data;


/**
 * <p>
 * 容器中玻璃绑定状态信息 前端控制器
 * </p>
 *
 * @author wujie
 * @since 2024-02-05
 */
@RestController

@RequestMapping("/api/material/lot/item")
@Api(value="容器中玻璃绑定状态信息控制层",tags={"容器中玻璃绑定状态信息操作接口"})
public class MaterialLotItemController {

    
    @Autowired
    private MaterialLotItemService materialLotItemService;

    
    /**
    * 通过id查询
    */
    @GetMapping("/getById/{id}")
    @ApiOperation(value="获取容器中玻璃绑定状态信息",notes="获取容器中玻璃绑定状态信息")
    public ResultBody getById(@PathVariable(value = "id") Integer id){
        return ResultBody.success(materialLotItemService.getById(id));
    }

    /**
    * 新增
    */
    @PostMapping("/save")
    @ApiOperation(value="新增容器中玻璃绑定状态信息",notes="新增容器中玻璃绑定状态信息")
    public ResultBody save(@RequestBody MaterialLotItem materialLotItem){
        materialLotItemService.save(materialLotItem);
        return ResultBody.success();
    }

    /**
    * 通过id删除
    */
    @GetMapping("/deleteById/{id}")
    @ApiOperation(value="通过ID删除容器中玻璃绑定状态信息",notes="通过ID删除容器中玻璃绑定状态信息")
    public ResultBody delete(@PathVariable(value = "id") String ids){
        String[] idsStrs = ids.split(",");
        for (String id:idsStrs){
            materialLotItemService.removeById(Integer.parseInt(id));
        }
        return ResultBody.success();
    }

    /**
    * 修改
    */
    @PostMapping("/update")
    @ApiOperation(value="修改容器中玻璃绑定状态信息",notes="修改容器中玻璃绑定状态信息")
    public ResultBody updateById(@RequestBody MaterialLotItem materialLotItem){
        materialLotItemService.updateById(materialLotItem);
        return ResultBody.success();
    }


    /**
    * 查询列表
    */
    @PostMapping("/list")
    @ApiOperation(value="非分页列表查询容器中玻璃绑定状态信息",notes="非分页列表查询容器中玻璃绑定状态信息")
    public ResultBody list(@RequestBody MaterialLotItemReqVo materialLotItem ){
    final LambdaQueryWrapper<MaterialLotItem> lambda = new QueryWrapper<MaterialLotItem>().lambda();
        this.buildCondition(lambda,materialLotItem);
        return ResultBody.success(materialLotItemService.list(lambda));
    }

    /**
    * 分页查询
    */
    @PostMapping("/page")
    @ApiOperation(value="分页列表查询容器中玻璃绑定状态信息",notes="分页列表查询容器中玻璃绑定状态信息")
    public ResultBody page(@RequestBody PageRequest<MaterialLotItemReqVo> pageParam){
        final MaterialLotItemReqVo param = pageParam.getParam();
        final LambdaQueryWrapper<MaterialLotItem> lambda = new QueryWrapper<MaterialLotItem>().lambda();
        this.buildCondition(lambda,param);
        final IPage<MaterialLotItem> page = materialLotItemService.page(new Page<>(pageParam.getPageNo(), pageParam.getPageSize()), lambda);
        PageResult<MaterialLotItem> pr = new PageResult(page);
        return ResultBody.success(pr);
    }




    @GetMapping("/queryGlassBindByParams/{bindId}")
    @ApiOperation(value="根据绑定Id获取玻璃绑定信息",notes="根据绑定Id获取玻璃绑定信息")
    public ResultBody queryGlassBindByParams(@PathVariable(value = "bindId") String bindId){
        return ResultBody.success(materialLotItemService.queryGlassBindByParams(bindId));
    }


    @GetMapping("/getCountByBindId/{bindId}")
    @ApiOperation(value="根据绑定Id获取玻璃绑定数量",notes="根据绑定Id获取玻璃绑定数量")
    public ResultBody getCountByBindId(@PathVariable(value = "bindId") String bindId){
        return ResultBody.success(materialLotItemService.getCountByBindId(bindId));
    }


    /**
        * 构造查询条件
        * @param lambda
        * @param param
        */
        private void buildCondition(LambdaQueryWrapper<MaterialLotItem> lambda, MaterialLotItemReqVo param){
            if(null == param) return;
            if(!StringUtils.isEmpty(param.getItemId())){
                lambda.eq(MaterialLotItem::getItemId, param.getItemId());
            }
            if(!StringUtils.isEmpty(param.getContentLotId())){
                lambda.eq(MaterialLotItem::getContentLotId, param.getContentLotId());
            }
            if(!StringUtils.isEmpty(param.getBindId())){
                lambda.eq(MaterialLotItem::getBindId, param.getBindId());
            }
            if(!StringUtils.isEmpty(param.getGlassCode())){
                lambda.eq(MaterialLotItem::getGlassCode, param.getGlassCode());
            }
            if(!StringUtils.isEmpty(param.getGlassSerialNumber())){
                lambda.eq(MaterialLotItem::getGlassSerialNumber, param.getGlassSerialNumber());
            }
            if(!StringUtils.isEmpty(param.getStatus())){
                lambda.eq(MaterialLotItem::getStatus, param.getStatus());
            }
            if(!StringUtils.isEmpty(param.getGlassStatus())){
                lambda.eq(MaterialLotItem::getGlassStatus, param.getGlassStatus());
            }
            if(!StringUtils.isEmpty(param.getHandOperationStatus())){
                lambda.eq(MaterialLotItem::getHandOperationStatus, param.getHandOperationStatus());
            }
            if(!CollectionUtils.isEmpty(param.getHandOperationTimeList())){
                lambda.ge(MaterialLotItem::getHandOperationTime, param.getHandOperationTimeList().get(0));
                lambda.le(MaterialLotItem::getHandOperationTime, param.getHandOperationTimeList().get(1));
            }
            if(!StringUtils.isEmpty(param.getBindTime())){
                lambda.eq(MaterialLotItem::getBindTime, param.getBindTime());
            }
            if(!StringUtils.isEmpty(param.getUnbindTime())){
                lambda.eq(MaterialLotItem::getUnbindTime, param.getUnbindTime());
            }
            if(!StringUtils.isEmpty(param.getUnbindRemark())){
                lambda.eq(MaterialLotItem::getUnbindRemark, param.getUnbindRemark());
            }
            if(!StringUtils.isEmpty(param.getIsTraceMark())){
                lambda.eq(MaterialLotItem::getIsTraceMark, param.getIsTraceMark());
            }
            if(!StringUtils.isEmpty(param.getCreateTime())){
                lambda.eq(MaterialLotItem::getCreateTime, param.getCreateTime());
            }
            if(!StringUtils.isEmpty(param.getCreateUser())){
                lambda.eq(MaterialLotItem::getCreateUser, param.getCreateUser());
            }
            if(!CollectionUtils.isEmpty(param.getUpdateTimeList())){
                lambda.ge(MaterialLotItem::getUpdateTime, param.getUpdateTimeList().get(0));
                lambda.le(MaterialLotItem::getUpdateTime, param.getUpdateTimeList().get(1));
            }
            if(!StringUtils.isEmpty(param.getUpdateUser())){
                lambda.eq(MaterialLotItem::getUpdateUser, param.getUpdateUser());
            }
            if(!StringUtils.isEmpty(param.getIsUnbind())){
                lambda.eq(MaterialLotItem::getIsUnbind, param.getIsUnbind());
            }
            if(!StringUtils.isEmpty(param.getUnbindUser())){
                lambda.eq(MaterialLotItem::getUnbindUser, param.getUnbindUser());
            }
            lambda.orderBy(true,false, MaterialLotItem::getItemId);
        }


        /**
         * 请求model
         */
        @Data
        @AllArgsConstructor
        private static class MaterialLotItemReqVo extends MaterialLotItem {
            private List<String> handOperationTimeList; // 人工操作时间起止
            private List<String> updateTimeList; // 更新时间起止
        }


}
